﻿// Client.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \
        const type name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}
MIDL_DEFINE_GUID(IID, IID_ICSharpCompiler,0x5C03BD52,0x6DA4,0x4436,0xB0,0xAC,0x7B,0x70,0xD0,0xBE,0x58,0xCD);
MIDL_DEFINE_GUID(CLSID, CLSID_CSharpCompiler,0x3D9277DF,0xB964,0x4BD8,0xA5,0xC7,0xE6,0x9E,0x08,0xF3,0xB0,0xD1);

typedef HRESULT (WINAPI *DllGetClassObjectType)(
  __in   REFCLSID rclsid,
  __in   REFIID riid,
  __out  LPVOID *ppv
);

class ICSharpCompiler : public IUnknown
{
public:
    virtual HRESULT STDMETHODCALLTYPE Compile( 
        /* [in] */ BSTR code,
        /* [in] */ BSTR output) = 0;
        
};

int _tmain(int argc, _TCHAR* argv[])
{
	HRESULT hr;
	IClassFactory* pFactory;
	ICSharpCompiler* pCC;

	HMODULE hLib = LoadLibraryW(L"COMTest.dll");
	FARPROC pFunc = GetProcAddress(hLib, "DllGetClassObject");
	hr = ((DllGetClassObjectType)pFunc)(CLSID_CSharpCompiler, IID_IClassFactory, (LPVOID*)&pFactory);
	//hr = ((DllGetClassObjectType)pFunc)(CLSID_NULL, IID_IClassFactory, (LPVOID*)&pFactory);
	if(SUCCEEDED(hr))
	{
		hr = pFactory->CreateInstance(NULL, IID_ICSharpCompiler, (LPVOID*)&pCC);
		pFactory->Release();
		if(SUCCEEDED(hr))
		{
			pCC->Compile(L"Hello COM!", L"c:\\d\\test123.txt");
			pCC->Release();
		}
	}

	FreeLibrary(hLib);
	return 0;
}

